/**
 * 作者 吴佩雄
 */
const express = require("express");
const router = express.Router();
const db = require("../model/db");

//添加电影
router.post("/", async function (req, res) {
    const data = await db.Movie.create(req.body);
    res.send(data);
});


//添加导演
router.post("/director/:id", async (req, res) => {
    let data = await db.Movie.findById(req.params.id);
    data.director.push(req.body);
    await db.Movie.updateOne({ _id: db.ObjectId(req.params.id) }, data);
    res.send(true);
})

//删除导演
router.put("/director/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    data.director.forEach(d => {
        if (d._id == req.body.key) {
            data.director.remove(d);
        }
    });
    await db.Movie.updateOne({ _id: req.params.id }, data);
    res.send(true);
})

//演员数组
router.post("/actor/:id", async (req, res) => {
    let data = await db.Movie.findById(req.params.id);
    data.actor.push(req.body);
    await db.Movie.updateOne({ _id: db.ObjectId(req.params.id) }, data);
    res.send(true);
})
//演员数组删除
router.put("/actor/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    data.actor.forEach(d => {
        if (d._id == req.body.key) {
            data.actor.remove(d);
        }
    });
    await db.Movie.updateOne({ _id: req.params.id }, data);
    res.send(true);
})

//剧照添加
router.post("/images/:id", async (req, res) => {
    let data = await db.Movie.findById(req.params.id);
    console.log(req.body.images);
    data.images.push(req.body.images);
    await db.Movie.updateOne({ _id: db.ObjectId(req.params.id) }, data);
    res.send(true);
})
//剧照数组删除
router.put("/images/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);

    data.images.pop(req.body.key)


    await db.Movie.updateOne({ _id: req.params.id }, data);
    res.send(true);
})




//删除电影
router.delete("/:id", async (req, res) => {
    await db.Movie.deleteOne({
        _id: db.ObjectId(req.params.id),
    });//条件对象
    res.send(true);
})


// 修改电影
router.put("/:id", async (req, res) => {
    await db.Movie.updateOne({//条件对象
        _id: req.params.id
    }, req.body);
    res.send(true);
})

//查询所有
router.get("/", async (req, res) => {
    //处理参数
    const query = Object.assign({},
        {
            type: "",
            area: "",
            charactor: "none",
            upDate: "",
            key: "",
            page: 1,
            limit: 12,
            sort: "_id"
        }, req.query);
    //根据参数完成条件对象
    const condition = {};
    //类型的条件
    condition.type = { $regex: new RegExp(query.type) };
    //区域的条件
    condition.area = { $regex: new RegExp(query.area) };
    //上映时间的条件
    condition.upDate = { $regex: new RegExp(query.upDate) };
    //电影上映类型的条件
    if (query.charactor == "isHot") {
        condition.isHot = true;
    } else if (query.charactor == "isComing") {
        condition.isComing = true;
    } else if (query.charactor == "isClassic") {
        condition.isClassic = true;
    }
    //关键字
    const keyReg = new RegExp(query.key);
    condition.$or = [
        { name: { $regex: keyReg } },
        { ename: { $regex: keyReg } }
    ];
    //查询
    const data = await db.Movie.find(condition).sort(query.sort).skip((query.page - 1) * query.limit).limit(query.limit * 1);
    const count = await db.Movie.countDocuments(condition);
    res.send({
        code: 0,
        message: "",
        count,
        data
    })
})
//查询单个
router.get("/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    const count = await db.Movie.countDocuments();
    res.send({
        code: 0,
        message: "",
        count,
        data
    });
})


// 正在热映 
router.post("/isHot/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    if (data.isHot == true) {
        data.isHot = false;
    } else {
        data.isHot = false;
    }
    await db.Movie.updateOne({ _id: db.ObjectId(req.params.id) }, data);
    res.send(true);
})

//即将上映 
router.post("/isComing/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    if (data.isComing == true) {
        data.isComing = false;
    } else {
        data.isComing = false;
    }
    await db.Movie.updateOne({ _id: db.ObjectId(req.params.id) }, data);
    res.send(true);
})

//经典
router.post("/isClassic/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    if (data.isClassic == true) {
        data.isClassic = false;
    } else {
        data.isClassic = false;
    }
    await db.Movie.updateOne({ _id: db.ObjectId(req.params.id) }, data);
    res.send(true);
})
module.exports = router;